# -*- coding: utf-8 -*-
#
#    Copyright (C) 2012 Gonzalo Bonigo <gonzalobonigo@gmail.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see http://www.gnu.org/licenses/
#


"""Este modulo contiene la definicion de las clases que reprecentan estados
en un LTS, Diagnoser o TwinPlant."""


class State(object):
    """Esta clase representa el estado de un LTS."""

    def __init__(self, name, init=False):
        self.name = name
        self.initial = init

    def __eq__(self, state):
        return (self.name == state.name) and (self.initial == state.initial)

    def __str__(self):
        return self.name

    def __hash__(self):
        return hash(self.name)


class DiagnoserState(State):

    def __init__(self, *args):
        super(DiagnoserState, self).__init__(*args)
        self.fault_info = set()

    def __eq__(self, state):
        super_cmp = super(DiagnoserState, self).__eq__(state)
        return super_cmp and (self.fault_info == state.fault_info)

    def __str__(self):
        result = super(DiagnoserState, self).__str__()
        result += '#('
        for fault in self.fault_info:
            if result.endswith('('):
                result += fault.name
            else:
                result += ',' + fault.name
        result += ')'
        return result


class TwinPlantState(State):

    def __init__(self, *args):
        super(DiagnoserState, self).__init__(*args)
        self.left_state = None
        self.right_state = None

    def __eq__(self, state):
        super_cmp = super(DiagnoserState, self).__eq__(state)
        left_cmp = self.left_state == state.left_state
        right_cmp = self.right_state == state.right_state
        return super_cmp and left_cmp and right_cmp

    def __str__(self):
        result = super(DiagnoserState, self).__str__()
        result += '#L('
        if self.left_state is not None:
            for fault in self.left_state.fault_info:
                if result.endswith('('):
                    result += fault.name
                else:
                    result += ',' + fault.name
        result += ')#R('
        if self.right_state is not None:
            for fault in self.right_state.fault_info:
                if result.endswith('('):
                    result += fault.name
                else:
                    result += ',' + fault.name
        result += ')'
        return result
